package com.zcxx.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zcxx.mapper.EmployeeMapper;
import com.zcxx.model.Employee;
import com.zcxx.service.EmployeeService;
import com.zcxx.utils.FileStorageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Autowired
    private FileStorageUtil fileStorageUtil;

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 添加员工信息
     *
     * @return 返回操作结果
     */
    @Transactional
    @Override
    public String saveEmployee(Employee employee, MultipartFile photo) throws IOException {
            // 1. 上传照片
            String photoName = fileStorageUtil.storeFile(photo);

            // 2. 创建 Employee 对象并设置字段
            employee.setPhoto(photoName);

            // 3. 插入员工信息
            int result = employeeMapper.insert(employee);

            if (result > 0) {
                return "员工信息插入成功！";
            } else {
                throw new RuntimeException("员工信息插入失败！");
            }
    }

    @Transactional
    @Override
    // 更新员工信息
    public String updateEmployee(Employee employee, MultipartFile photo) throws IOException{
        // 1. 获取员工对象
        Employee oldemployee = employeeMapper.selectById(employee.getId());
        if (oldemployee == null) {
            throw new RuntimeException("员工不存在！");
        }

        // 2. 如果有新的照片，则更新照片
        if (photo != null && !photo.isEmpty()) {
            // 删除旧照片
            String oldPhotoName = oldemployee.getPhoto();
            if (oldPhotoName != null && !oldPhotoName.isEmpty()) {
                fileStorageUtil.deleteFile(oldPhotoName);
            }
            // 上传新照片
            String photoName = fileStorageUtil.storeFile(photo);
            employee.setPhoto(photoName);
        }

        // 4. 更新员工记录
        int result = employeeMapper.updateById(employee);
        if (result > 0) {
            return "员工信息更新成功！";
        } else {
            throw new RuntimeException("员工信息更新失败！");
        }
    }

    /**
     * 根据ID删除员工
     *
     * @param id 员工ID
     * @return 返回操作结果
     */
    @Transactional
    @Override
    public String deleteEmployeeById(Integer id) throws IOException{
            // 1. 获取员工对象
            Employee employee = employeeMapper.selectById(id);
            if (employee == null) {
                throw new RuntimeException("员工不存在！");
            }

            // 2. 删除员工照片文件
            String photoName = employee.getPhoto();
            if (photoName != null && !photoName.isEmpty()) {
                fileStorageUtil.deleteFile(photoName);
            }

            // 3. 删除员工记录
            int result = employeeMapper.deleteById(id);
            if (result > 0) {
                return "员工信息删除成功！";
            } else {
                throw new RuntimeException("员工信息删除失败！");
            }
    }
}
